package com.example.apple.printingroommanagementsystem;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by apple on 17/3/3.
 */
public class BusinessTypeListHomePageActivity extends Activity {
    ListView listView;
    BaseAdapter myAdapater;
    ArrayList<MediaItem>mediaItems;
    SharedPreferences preferences;
    ImageView businessTypeList_homePage_return;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.businesstypelist_homepage);
        listView  = (ListView) findViewById(R.id.businessTypeList_homePage_list);
        preferences =getSharedPreferences("ure",Activity.MODE_PRIVATE);
        businessTypeList_homePage_return =(ImageView) findViewById(R.id.businessTypeList_homePage_return);
        businessTypeList_homePage_return.setOnClickListener(new onClickretur());
        setBusinessData();
    }

    private void setBusinessData() {
        GetRequest get = new GetRequest();
        get.doGet("http://a.xl18z.cn/api/business/list","?token="+preferences.getString("token",""),new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                mediaItems = new ArrayList<MediaItem>();
                JSONObject JS =(JSONObject) msg.obj;
                try {
                    JSONArray  jsarr= JS.getJSONArray("record");
                    for (int i =0;i<jsarr.length();i++){
                        JSONObject json = jsarr.getJSONObject(i);
                        MediaItem mediaItem = new MediaItem();
                        mediaItem.id = json.getString("id");
                        mediaItem.name =json.getString("name");
                        mediaItem.unitPrice = json.getString("unitPrice");
                        mediaItems.add(mediaItem);
                    }
                    bindingData();
                    listView.setAdapter(myAdapater);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    void bindingData(){
        myAdapater = new BaseAdapter() {

            @Override
            public int getCount() {
                return mediaItems.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHoder viewHoder;
                if (convertView==null){
                    convertView = View.inflate(getWindow().getContext(),R.layout.businesstypelisthomepage_item,null);
                    viewHoder =new ViewHoder();
                    viewHoder.id = (TextView) convertView.findViewById(R.id.business_id);
                    viewHoder.name = (TextView)convertView.findViewById(R.id.business_name);
                    viewHoder.unitPrice = (TextView)convertView.findViewById(R.id.business_unitPrice);
                    convertView.setTag(viewHoder);
                }else {
                    viewHoder =(ViewHoder) convertView.getTag();
                }
                MediaItem mediaItem =  mediaItems.get(position);
                viewHoder.id.setText(mediaItem.id);
                viewHoder.name.setText(mediaItem.name);
                viewHoder.unitPrice.setText(mediaItem.unitPrice);
                return convertView;
            }
        };
    }
   public class ViewHoder {
        TextView id,name,unitPrice;
    }

    private class onClickretur implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            BusinessTypeListHomePageActivity.this.finish();
        }
    }
}
